import redis
import threading
import os
import sys
import logging
import logging.handlers

class Listener(threading.Thread):
   def __init__(self, r, channels,devRedis):
     self.log = logging.getLogger()
     self.log.setLevel(logging.DEBUG)
     h1 = logging.handlers.RotatingFileHandler('redis.log',maxBytes=2097152,backupCount=5)
     h1.setLevel(logging.INFO)
     f=logging.Formatter('%(asctime)s %(levelname)s %(message)s')
     h1.setFormatter(f)
     self.log.addHandler(h1)
     threading.Thread.__init__(self)
     self.redis = r
     self.devRedis=devRedis
     self.pubsub = self.redis.pubsub()
     self.pubsub.subscribe(channels)
   def work(self, item):
       #self.log.info(item['data'])
     self.devRedis.publish('Rcs'+chr(1)+'WorkHint'+chr(1)+'QDR',item['data'])
     print item['data']
   def run(self):
     for item in self.pubsub.listen():
       if item['data'] == "KILL":
         self.pubsub.unsubscribe()
         print self, "unsubscribed and finished"
         break
       else:
         self.work(item)
 
if __name__ == "__main__":
    normalRedis = redis.Redis(host='192.168.91.17', port=6379, db=0)
    devRedis = redis.Redis(host='192.168.0.94', port=6379, db=0)
    client = Listener(normalRedis, ['Rcs'+chr(1)+'WorkHint'+chr(1)+'QDR'],devRedis)
    client.start()
